#include <QMessageBox>
#include <QTimer>
#include <QDateTime>
#include <QProcess>
#include <QtGui>
#include "form.h"

Form::Form(QWidget *parent) :
    QWidget(parent)
//, m_ui(new Ui::Form)
{
    setupUi(this);
    connect(this, SIGNAL(enableAll()), this, SLOT(onEnableAll()) );
    connect(this, SIGNAL(disableAll()), this, SLOT(onDisableAll()) );
    connect(this, SIGNAL(fire()), this, SLOT(onFire()) );
    timer = new QTimer(this);
    time = QDateTime::currentDateTime();
    connect(this, SIGNAL(enterPressed()), this, SLOT(onStart()) );
    connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout()) );
}

Form::~Form()
{
//    delete m_ui;
}

void Form::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        retranslateUi(this);
        break;
    default:
        break;
    }
}

void Form::onStart(){
    QDateTime current = QDateTime::currentDateTime();
    if(!m_downCount->isChecked()){
        time.setDate( current.date() );
        time.setTime( QTime(spin_hour->value(),
                            spin_minute->value(),
                            spin_second->value() ));
        if(time < current){
            time = time.addDays(1);
        }
    }
    else{
        time = current.addSecs( ( spin_hour->value() *60
                                  + spin_minute->value() )*60
                                + spin_second->value() );
    }

    if(!current.secsTo(time)){
        QMessageBox msg(QMessageBox::NoIcon, "Confirm",
                        "Are you sure you want to shutdown now?",
                        QMessageBox::Yes | QMessageBox::Cancel, this);
        if(msg.exec() == QMessageBox::Cancel )
            return;
        emit fire();
        return;
    }

    emit disableAll();

    timer->start(1000);
}

void Form::onCancel(){
    timer->stop();
    emit enableAll();
}

#pragma warning(disable :4100)
void Form::onDownCountChanged(bool b ){
#pragma warning(default :4100)
//    if(!b){
//    QMessageBox::information(this,
//                             "Info",
//                             "Currently, don't support this."
//                             );
//    }
}

void Form::onDisableAll(){
    btn_start->setEnabled(false);
    btn_cancel->setEnabled(true);
    spin_hour->setEnabled(false);
    spin_minute->setEnabled(false);
    spin_second->setEnabled(false);
    m_downCount->setEnabled(false);
}

void Form::onEnableAll(){
    btn_start->setEnabled(true);
    btn_cancel->setEnabled(false);
    spin_hour->setEnabled(true);
    spin_minute->setEnabled(true);
    spin_second->setEnabled(true);
    m_downCount->setEnabled(true);
}

void Form::onTimeout(){
    if( QDateTime::currentDateTime().secsTo(time) <= 1){
        timer->stop();
        emit fire();

        return;
    }
    if(m_downCount->isChecked())
            updateTime();
    //timer->start(1000);
}

void Form::updateTime(){
    if(m_downCount->isChecked()){
        QDateTime currenttime = QDateTime::currentDateTime();
        int v = currenttime.secsTo(time);

        spin_hour->setValue(v / 60 / 60);
        spin_minute->setValue(v /60 % 60);
        spin_second->setValue(v % 60);
    }
}

void Form::onFire(){
#ifdef _DEBUG
    QMessageBox::information(this, "Run", "Success!");
#else
    QObject obj;
    QProcess * process = new QProcess(&obj);

    process->start("shutdown -s -t 0");
    process->waitForFinished(1000);
#endif
}

void Form::keyPressEvent ( QKeyEvent * event ){
    switch(event->key()){
    case Qt::Key_Enter:
    case Qt::Key_Return:
        emit enterPressed();
    }
    QWidget::keyPressEvent(event);    
}
